package Interview.Amazon;

import domain.TreeNode;
import java.util.LinkedList;

/**
 * https://mp.csdn.net/console/editor/html/107591979
 */
public class Test01二叉树转为镜像二叉树 {
    //非递归 广度优先遍历
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        if (root.left == null && root.right == null) {
            return root;
        }

        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        while (!list.isEmpty()) {
            TreeNode node = list.remove();
            if (node.left == null && node.right == null) {
                continue;
            }
            //交换左右节点
            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;

            if (node.left != null) {
                list.add(node.left);
            }
            if (node.right != null) {
                list.add(node.right);
            }
        }
        return root;

    }

    //递归
    // public TreeNode mirrorTree(TreeNode root) {
    //      if (root == null) {
    //          return null;
    //      }

    //      if (root.left == null && root.right == null) {
    //          return root;
    //      }

    //      //交换左右节点
    //      TreeNode temp = root.left;
    //      root.left = root.right;
    //      root.right = temp;

    //      //递归左右子树
    //      mirrorTree(root.left);
    //      mirrorTree(root.right);
    //      return root;
    // }
}
